


function getCookie(key){
	const cookie = document.cookie
	// a=100000; b=200; c=300
	const arr = cookie.split('; ')
	// 拿到的是一个数组 此时继续遍历循环数组 把数组里面的每一项进行切割
	for(let i = 0 ; i < arr.length ; i++){
		const arr1 = arr[i].split('=')
		// console.log(arr1)
		if(arr1[0] === key){
			return arr1[1]
		}
	}
	return null
}
function setCookie(key,value,expires){
	const date = new Date()
	if(expires){
		const time = date.getTime() - 8 * 60 * 60 * 1000 + expires * 1000
		date.setTime(time)
	}
	document.cookie = `${key}=${value};path=/;expires=${expires ? date : ''}`
}

require(['./config'],()=>{
    require(['template','request','header','footer','jquery'],(template,req)=>{
        
        $("#username")[0].oninput = function(){  
            var uid = $("#username").val();
            // alert(111)
            // console.log(uid)
            if(uid == ''){
                $("#ts").html("*不能为空！");
                $("#ts").css("color","red");
                // return false;
            }else if(uid == getCookie('userInfo')){
                    $("#ts").html("*用户名已注册！");
                    $("#ts").css("color","red");
                    return false 
            }else{
                 //手机验证
                var phoneReg = /^1[3-9][0-9]{9}$/;
                if(!phoneReg.test(uid)){
                    $("#ts").html("*请输入正确的手机号！");
                    $("#ts").css("color","red");
                    return false;
                }else{
                    $("#ts").html("√");
                    $("#ts").css("color","green");
                    return false;
                 }
                }
            }
            // 设置密码
            $("#password")[0].oninput = function(){
                var pwd = $("#password").val();
                if(pwd == ''){
                    $("#pwd").html("*密码不能为空！");
                    $("#pwd").css("color","red");
                    return false;
                }else{
                    $("#pwd").html("√");
                    $("#pwd").css("color","green");
                   
                } 
            }
            // 确认密码
            $("#passwords")[0].oninput = function(){
                var pwds = $("#passwords").val();
                var pwd = $("#password").val();
                if(pwds == ''){
                    $("#pwds").html("*确认密码不能为空！");
                    $("#pwds").css("color","red");
                    return false;
                }else if(pwds !== pwd){
                    $("#pwds").html("*输入密码不一致！");
                    $("#pwds").css("color","red");
                    return false;
                }else{
                    $("#pwds").html("√");
                    $("#pwds").css("color","green");
                }
                
            }
            var uid = $("#username").val();
            var pwd = $("#password").val();
            // console.log(uid)
            $('.btn_reg').click(function(){
               
                // setCookie('userInfo',uid,604800)
                // setCookie('userPwd',pwd,604800)
                    alert('注册成功')
                    location.href = '/html/login.html'
                    // console.log( setCookie('userInfo'))
            })
            //动态码验证
            $("#uReg")[0].oninput = function(){
                var reg = $("#uReg").val();
                if(reg == ''){
                    $("#dtm").html("*验证码不能为空！");
                    $("#dtm").css("color","red");
                    return false;
                }else if(reg){
                    var regNumber = /^[0-9]{6}$/;
                    if(!regNumber.test(reg)){
                        $("#dtm").html("*请输入正确的动态码！");
                        $("#dtm").css("color","red");
                        return false;
                    }else{
                        $("#dtm").html("√");
                        $("#dtm").css("color","green");
                    
                    }
                }else{
                    $("#dtm").html("√");
                    $("#dtm").css("color","green");
                }
                
            }
           
    })

        
 })


   
